/*
 * Nome do arquivo: RecebeDadoscolaborador.java
 * Autores:	Bruno Villas Bôas           - 317527
 *              Diego Zamboni               - 344990
 *              Endel Guimarães             - 379930
 * 		Guilherme Torres            - 400572
 *		Renan Oliveira              - 380113
 * 
 * Turma: 2012/1
 *
 * Descricao: Servlet responsável pelo gerenciamento de colaboradores
 *
 * Data de conclusao: 
 */
package cooper.classes.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.jmx.remote.internal.ArrayQueue;
import cooper.classes.bean.ServicoBean;
import cooper.classes.dao.CooperDAOException;
import cooper.classes.dao.ServicoDAO;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;

public class RecebeDadosServico extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String acao = request.getParameter("acao");

        if (acao.compareTo("cadastrar") == 0) {
            try {
                acaoCadastrar(request, response);
            } catch (CooperDAOException ex) {
                Logger.getLogger(RecebeDadosServico.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (acao.compareTo("alterar") == 0) {
            try {
                acaoAlterar(request, response);
            } catch (CooperDAOException ex) {
                Logger.getLogger(RecebeDadosServico.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (acao.compareTo("remover") == 0) {
            try {
                acaoRemover(request, response);
            } catch (CooperDAOException ex) {
                Logger.getLogger(RecebeDadosServico.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (acao.compareTo("consultar") == 0) {
            int idCoop = Integer.parseInt(request.getParameter("idCoop"));
            acaoConsultar(request, response, idCoop);
        } else if (acao.compareTo("ajudarColaborador") == 0) {
            int idColab = Integer.parseInt(request.getParameter("idColab"));
            int idServico = Integer.parseInt(request.getParameter("idServico"));
            acaoAjudaColaborador(request, response, idServico, idColab);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String acao = request.getParameter("acao");

        if (acao.compareTo("remover") == 0) {
            try {
                acaoRemover(request, response);
            } catch (CooperDAOException ex) {
                Logger.getLogger(RecebeDadosServico.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (acao.compareTo("alterarRealizado") == 0) {
            acaoAlterarRealizado(request, response);

        } else if (acao.compareTo("consultar") == 0) {
            int idCoop = Integer.parseInt(request.getParameter("idCoop"));
            acaoConsultar(request, response, idCoop);
        } else if (acao.compareTo("ajudarColaborador") == 0) {
            int idColab = Integer.parseInt(request.getParameter("idColab"));
            int idServico = Integer.parseInt(request.getParameter("idServico"));
            acaoAjudaColaborador(request, response, idServico, idColab);
        }
    }

    private void acaoCadastrar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, CooperDAOException {
        ServicoBean objservico = new ServicoBean();

        objservico.setProblema(request.getParameter("problema"));
        objservico.setDescricao(request.getParameter("descricao"));
        objservico.setTipoProblema(Integer.parseInt(request.getParameter("tipoProblema")));
        objservico.setIdCooperativa(Integer.parseInt(request.getParameter("idcooperativa")));

        //bean = objservico;

        String html = "";

        // Instanciando a colaborador e permitindo que ela seja vista na view
        try {
            ServicoDAO servico = new ServicoDAO();
            int idUsuario = servico.cadastrar(objservico);
            request.setAttribute("servicoBean", objservico);
            html = "Sucesso " + request.getParameter("problema");
        } catch (CooperDAOException e) {
            request.setAttribute("servicoBean", null);
            html = "Erro: " + e.getMessage();
        }

        //PrintWriter out = response.getWriter(); 
        //out.print(html); 
        //out.close();
        response.sendRedirect("indexcoop.jsp?cadastro=ok");
    }

    private void acaoAlterar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, CooperDAOException {
        ServicoBean objservico = new ServicoBean();

        objservico.setProblema(request.getParameter("problema"));
        objservico.setDescricao(request.getParameter("descricao"));
        objservico.setTipoProblema(Integer.parseInt(request.getParameter("tipoProblema")));

        try {
            ServicoDAO servico = new ServicoDAO();
            servico.alterar(objservico);
            request.setAttribute("colaboradorBean", objservico);
        } catch (CooperDAOException e) {
            request.setAttribute("colaboradorBean", null);
        }

        response.sendRedirect("indexcoop.jsp");
    }

    private void acaoRemover(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, CooperDAOException {

        response.sendRedirect("indexcoop.jsp");
    }

    private void acaoConsultar(HttpServletRequest request, HttpServletResponse response, int idCoop)
            throws ServletException, IOException {
        try {
            ServicoDAO servico = new ServicoDAO();
            int pagina;
            try {
                pagina = Integer.parseInt(request.getParameter("page"));
            } catch (Exception e) {
                pagina = 1;
            }
            ArrayList<ServicoBean> novo = servico.consultarCoopServico(idCoop, pagina - 1);

            int total = servico.getTotal();

            request.setAttribute("total", total);
            request.setAttribute("servicoBean", novo);
        } catch (Exception sqle) {
            request.setAttribute("servicoBean", null);
            request.setAttribute("msg", sqle.getMessage());
        }

        RequestDispatcher rd = null;

        // Tem que redirecionar para o view certo
        rd = request.getRequestDispatcher("/visualizaservico.jsp");
        rd.forward(request, response);
    }
    public ServicoBean bean;

    private void acaoAlterarRealizado(HttpServletRequest request, HttpServletResponse response) throws IOException {

        int idServico = Integer.parseInt(request.getParameter("idServico"));
        int idColab = Integer.parseInt(request.getParameter("idColab"));

        try {
            ServicoDAO servico = new ServicoDAO();
            servico.alterarRealizado(idServico, idColab);

        } catch (CooperDAOException e) {
        }

        response.sendRedirect("servicosrealizadosdetalhes_OK.jsp?cadastro=ok");
    }

    private void acaoAjudaColaborador(HttpServletRequest request, HttpServletResponse response, int idServico, int idColab)
            throws IOException {
        try {
            ServicoDAO servico = new ServicoDAO();
            servico.ajudaColaborador(idServico, idColab);

        } catch (CooperDAOException e) {
        }

        response.sendRedirect("servicosabertosprest.jsp?cadastro=ok");
    }
}
